﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

using TagLib;

using MusicPlayerLib.DataModel;

namespace MusicPlayerLib.Utils
{
    /// <summary>
    /// 该类用来处理同专辑封面相关的动作;
    /// </summary>
    public class CoverArtUtils
    {
        public static string coverFolderName = @"E:\Music\Covers";

        /// <summary>
        /// 根据给定的id和数据创建图像文件，创建成功返回图片的地址，
        /// 失败则返回空字符串;
        /// </summary>
        /// <param name="id"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string SaveCover(AudioFile af)
        {
            string id = MusicPlayerUtils.GetAlbumId(af.Album, af.Artist);
           
            string path = coverFolderName + "\\" + id + ".jpg";
            //查看专辑封面是否已经存在;
            if(System.IO.File.Exists(path))
                return path ;

            byte[] data = GetCoverBytes(af.Location);
            if (data==null||data.Length == 0)
                return string.Empty;

            FileStream pFileStream = null;
            try
            {
                pFileStream = new FileStream(coverFolderName + "\\" + id+".jpg", FileMode.OpenOrCreate);
                pFileStream.Write(data, 0, data.Length);

                return path;
            }
            catch (Exception e)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 根据给定的音频文件获得专辑封面的字节数据
        /// 无封面是返回null
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static byte[] GetCoverBytes(string path)
        {
            byte[] data=null;
            try
            {
                TagLib.File f = TagLib.File.Create(path);
                if (f.Tag.Pictures != null && f.Tag.Pictures.Length > 0)
                {
                    data = new byte[f.Tag.Pictures[0].Data.Count];

                    //string type=f.Tag.Pictures[0].Type.ToString();
                    //Console.WriteLine(type);
                    f.Tag.Pictures[0].Data.CopyTo(data,0);
                }
            }
            catch (Exception e)
            { }

            return data;
        }

        /// <summary>
        /// 从给定的URL将专辑封面保存到本地，
        /// 文件名有id+图片的扩展名组成;
        /// </summary>
        /// <param name="id"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string LoadCover(string id, string url)
        {

            string ret = string.Empty;
            //获得图片的扩展名;
            string extension=string.Empty;
            int index=url.LastIndexOf(".");
            if(index<0)
                return ret;
            else
            {
                extension=url.Substring(index);
            }
            string path=coverFolderName+"\\"+id+extension;

            try
            {
                //在这里采用同步的方法下载，会阻塞调用该方法的线程;
                WebClient wc = new WebClient();
                wc.DownloadFile(new Uri(url), path);
            }
            catch (System.Net.Sockets.SocketException se)
            {
                path = string.Empty;
            }
            catch (System.IO.IOException ie)
            {
                path = string.Empty;
            }
            return path;
        }
    }
}
